﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Kooboo.HtmlFunc.Function;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.HtmlFunc.Analysis.Visitors
{
    public class ListItemDataVisitor : StartEndVisitor
    {
        public ListItemDataVisitor(VisitorContext context)
            : base(context, FuncType.ListItemData)
        {
        }

        private IFuncTester _startCommentTester;
        protected override IFuncTester StartCommentTester
        {
            get
            {
                if (_startCommentTester == null)
                    _startCommentTester = new Testers.ParametricTester(this.Func.StartComment);
                return _startCommentTester;
            }
        }

        protected override object OnQualified(HtmlCommentNode startNode, HtmlCommentNode endNode)
        {
            var list = (IList)this.Context.Items[FuncType.List];

            var tester = (Testers.ParametricTester)this.StartCommentTester;
            var columnName = tester.GetParameter("columnName");
            if (string.IsNullOrEmpty(columnName))
            {
                if (!this.Context.ThrowException)
                    return null;
                throw new Exceptions.ParameterEmptyException(this.Context.FileName, startNode, "columnName");
            }

            var controlType = tester.GetParameter("controlType");
            if (!string.IsNullOrEmpty(controlType))
            {
                if (!AnalysisSetting.SupportControlTypes.Contains(controlType))
                {
                    if (this.Context.ThrowException)
                        throw new Exceptions.ControlNotSupportException(this.Context.FileName, startNode, controlType);
                    controlType = string.Empty;
                }
            }

            bool? showInGrid = null;
            var strShowInGrid = tester.GetParameter("showInGrid");
            if ("showInGrid".IgnoreCaseEquals(strShowInGrid) || "true".IgnoreCaseEquals(strShowInGrid))
                showInGrid = true;

            int? order = null;
            var intOrder = 0;
            if (int.TryParse(tester.GetParameter("order"), out intOrder))
                order = intOrder;

            var dataItem = Provider.Factory.GetProvider<IDataItem>();
            dataItem.StartNode = startNode;
            dataItem.EndNode = endNode;
            dataItem.ColumnName = columnName;
            dataItem.ShowInGrid = showInGrid;
            dataItem.ControlType = controlType;
            dataItem.Order = order;
            list.DataItems.Add(dataItem);

            // ret context mark
            return dataItem;
        }

        protected override FuncType RequireType
        {
            get
            {
                return FuncType.List;
            }
        }

        protected override FuncType ExclusiveType
        {
            get
            {
                return this.FuncType;
            }
        }
    }
}
